/*
 * Project Name: jmax
 * File Name: ParkingInit.java
 * Copyright: Copyright(C) 1985-2015 ZKTeco Inc. All rights reserved.
 */
package com.xdtech.parking.init;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xdtech.common.service.impl.BaseService;
import com.xdtech.common.utils.EmptyUtil;
import com.xdtech.core.init.SysInitOperation;
import com.xdtech.core.model.BaseModel;
import com.xdtech.parking.protocol.manager.DeviceProtocol;
import com.xdtech.parking.util.ParkingContants;
import com.xdtech.sys.model.CodeValue;
import com.xdtech.sys.model.DictionaryCode;
import com.xdtech.sys.model.MenuFunction;
import com.xdtech.sys.model.Params;

/**
 * 
 * @author max.zheng
 * @version TODO 添加版本
 * @see 相关类或方法，不需要请删除此行
 * @since 2015-6-18 下午2:34:59
 */
public class ParkingInit implements SysInitOperation{
	Log log = LogFactory.getLog(ParkingInit.class);

	/* (非 Javadoc)
	 * getInitOrder
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @return
	 * @see com.xdtech.core.init.SysInitOperation#getInitOrder()
	 */
	@Override
	public int getInitOrder()
	{
		return 24;
	}

	/* (非 Javadoc)
	 * initingToCache
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param arg0
	 * @see com.xdtech.core.init.SysInitOperation#initingToCache(com.xdtech.common.service.impl.BaseService)
	 */
	@Override
	public void initingToCache(BaseService<BaseModel> baseService)
	{
		//先在这里初始化设备相关信息，后续分离后单独处理
		DeviceProtocol.initingToCache(baseService);
	}

	/* (非 Javadoc)
	 * initingToDb
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param arg0
	 * @see com.xdtech.core.init.SysInitOperation#initingToDb(com.xdtech.common.service.impl.BaseService)
	 */
	@Override
	public void initingToDb(BaseService<BaseModel> baseService)
	{
		List params = (List) baseService.findByHql("from Params where code=? and codeValue=?", "ParkingInit","true");
		log.info("停车模块数据库初始化......");
		if (EmptyUtil.isEmpty(params)) {
			initData(baseService);
			Params p = new Params("ParkingInit", "true", "停车模块数据库初始化标记");
			baseService.save(p);
			log.info("停车模块数据库初始化......结束");
		}else {
			log.info("停车模块数据库已经初始化......完毕");
		}
	}

	/**
	 * 
	 * @author max.zheng
	 * @create 2015-6-20上午10:10:52
	 * @modified by
	 * @param baseService
	 */
	private void initData(BaseService<BaseModel> baseService) {
		MenuFunction m1 = new MenuFunction();
		m1.setIconName("icon-save");
		m1.setNameCN("停车场管理");
		m1.setOperationCode("parking-manage");
		m1.setPageUrl(null);
		baseService.save(m1);
		
		
		
		MenuFunction m11 = new MenuFunction();
		m11.setIconName("icon-save");
		m11.setNameCN("会员管理");
		m11.setOperationCode("parking-member-manage");
		m11.setPageUrl("parkUser.do?parkUser");
		m11.setParent(m1);
		baseService.save(m11);
		
		MenuFunction m12 = new MenuFunction();
		m12.setIconName("icon-save");
		m12.setNameCN("设备管理");
		m12.setOperationCode("parking-device-manage");
		m12.setPageUrl("parkingDevice.do?parkingDevice");
		m12.setParent(m1);
		baseService.save(m12);
		
		
		//设备状态
 		DictionaryCode dictionaryCode = new DictionaryCode("PARK_DEVICE_STATUS","停车场设备状态描述");
		baseService.save(dictionaryCode);
		baseService.save(new CodeValue(ParkingContants.PARK_DEVICE_DISABLE_STATUE+"","初始化",1,dictionaryCode));
		baseService.save(new CodeValue(ParkingContants.PARK_DEVICE_ON_STATUE+"","开/降",2,dictionaryCode));
		baseService.save(new CodeValue(ParkingContants.PARK_DEVICE_OFF_STATUE+"","关/升",3,dictionaryCode));
		
		dictionaryCode = new DictionaryCode("DEVICE_BILLING","设备计费方式");
		baseService.save(dictionaryCode);
		baseService.save(new CodeValue("1","固定计费",1,dictionaryCode));
		baseService.save(new CodeValue("2","按时计费",2,dictionaryCode));
		
		dictionaryCode = new DictionaryCode("PARK_USER_TYPE","停车注册用户类型");
		baseService.save(dictionaryCode);
		baseService.save(new CodeValue(ParkingContants.PARK_MANAGER_USER_TYPE,"管理用户",1,dictionaryCode));
		baseService.save(new CodeValue(ParkingContants.PARK_COMMON_USER_TYPE,"普通用户",2,dictionaryCode));
		
		dictionaryCode = new DictionaryCode("PARK_PAY_TYPE","停车订单支付类型");
		baseService.save(dictionaryCode);
		baseService.save(new CodeValue(ParkingContants.PARK_WEIXIN_TYPE,"微信",1,dictionaryCode));
		baseService.save(new CodeValue(ParkingContants.PARK_ZHIFUBAO_TYPE,"支付宝",2,dictionaryCode));
		baseService.save(new CodeValue(ParkingContants.PARK_BLANK_TYPE,"银联",3,dictionaryCode));
		
		dictionaryCode = new DictionaryCode("PARK_HASCAR_TYPE","车位是否有车");
		baseService.save(dictionaryCode);
		baseService.save(new CodeValue("0","无车",1,dictionaryCode));
		baseService.save(new CodeValue("1","有车",2,dictionaryCode));
		
		dictionaryCode = new DictionaryCode("PARK_CAR_SET","车位状态设置");
		baseService.save(dictionaryCode);
//		0:设备初始状态 1:开启状态 2:关闭状态 99:找不到设备
		baseService.save(new CodeValue("0","初始状态",1,dictionaryCode));
		baseService.save(new CodeValue("1","开/降",2,dictionaryCode));
		baseService.save(new CodeValue("2","关/升",3,dictionaryCode));
		baseService.save(new CodeValue("99","找不到设备",4,dictionaryCode));
	}

	/* (非 Javadoc)
	 * initingToWebApplication
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param baseService
	 * @see com.xdtech.core.init.SysInitOperation#initingToWebApplication(com.xdtech.common.service.impl.BaseService)
	 */
	@Override
	public void initingToWebApplication(BaseService<BaseModel> baseService)
	{
		
	}

}
